package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.runtai.entity.*;
import com.system.runtai.service.*;
import com.system.common.result.Result;
import com.system.runtai.utils.AuthContextUtil;
import com.system.runtai.utils.PromptInformationDown;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 关于润泰 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "关于润泰")
@RestController
@RequestMapping("/runtai/in-regard-of")
public class InRegardOfController {

    @Autowired
    private InRegardOfService inRegardOfService;

    @Autowired
    private ArticleCollectService articleCollectService;

    @Autowired
    private SecondaryCopywriterService secondaryCopywriterService;

    @Autowired
    private CompanyIntroductionService companyIntroductionService;

    @Autowired
    private CompanyDynamicService companyDynamicService;


    @Autowired
    private CompanyCaseService companyCaseService;


    @Autowired
    private CompanyAnnouncementsService companyAnnouncementsService;

    @ApiOperation(value = "关于润泰")
    @GetMapping("/getInRegardOfList")
    public Result<List<InRegardOf>> getInRegardOfList(){
        List<InRegardOf> list = inRegardOfService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "查看关于润泰详情")
    @GetMapping("/getInRegardOf/{id}")
    public Result<InRegardOf> getInRegardOfById(@PathVariable Integer id){

        InRegardOf byId = inRegardOfService.getById(id);

        return Result.ok(byId);
    }


    @ApiOperation(value = "查看关于润泰详情")
    @GetMapping("/getCollectById/{id}")
    public Result<InRegardOf> getCollectById(@PathVariable Integer id){
        String s = AuthContextUtil.get();


        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getAppletUserId,Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId,0).eq(ArticleCollect::getChildId,id).eq(ArticleCollect::getStartId,1);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        InRegardOf byId = inRegardOfService.getById(id);
        if (one!=null){
            byId.setCollect(1);
        }else {
            byId.setCollect(0);
        }

        return Result.ok(byId);
    }



    @ApiOperation(value = "查看文章详情")
    @GetMapping("/getCollectByArticle")
    public Result getCollectByArticle(@RequestParam(required = false) Integer inRegardOfId,@RequestParam(required = false) Integer startId,@RequestParam(required = false) Integer childId) {
        String s = AuthContextUtil.get();


        if (startId == 1) {
            if (inRegardOfId == 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, 0).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                InRegardOf byId = inRegardOfService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            } else if (inRegardOfId != 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, inRegardOfId).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                SecondaryCopywriter byId = secondaryCopywriterService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            }
        } else if (startId == 2) {
            if (inRegardOfId == 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, 0).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                CompanyIntroduction byId = companyIntroductionService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            } else if (inRegardOfId != 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, inRegardOfId).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                SecondaryCopywriter byId = secondaryCopywriterService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            }

        } else if (startId == 3) {
            if (inRegardOfId == 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, 0).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                CompanyDynamic byId = companyDynamicService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            } else if (inRegardOfId != 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, inRegardOfId).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                SecondaryCopywriter byId = secondaryCopywriterService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            }
        } else if (startId == 4) {
            if (inRegardOfId == 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, 0).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                CompanyCase byId = companyCaseService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            } else if (inRegardOfId != 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, inRegardOfId).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                SecondaryCopywriter byId = secondaryCopywriterService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            }
        } else if (startId == 5) {
            if (inRegardOfId == 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, 0).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                CompanyAnnouncements byId = companyAnnouncementsService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            } else if (inRegardOfId != 0) {
                LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getInRegardOfId, inRegardOfId).eq(ArticleCollect::getChildId, childId).eq(ArticleCollect::getStartId, startId);
                ArticleCollect one = articleCollectService.getOne(wrapper);

                SecondaryCopywriter byId = secondaryCopywriterService.getById(childId);
                if (one != null) {
                    byId.setCollect(1);
                } else {
                    byId.setCollect(0);
                }

                return Result.ok(byId);
            }
        }
        return Result.fail().message("暂无该类型");
    }
    @ApiOperation(value = "关于润泰二级")
    @GetMapping("/getSecondaryCopywriter/{id}")
    public Result<List<SecondaryCopywriter>> getSecondaryCopywriter(@PathVariable Integer id,@RequestParam(required = false) String title){
        if (StringUtils.isEmpty(title)) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 1).eq(SecondaryCopywriter::getMainArgumentId, id);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }else if (!StringUtils.isEmpty(title)){
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 1).eq(SecondaryCopywriter::getMainArgumentId, id).like(SecondaryCopywriter::getTitle,title);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }

        List<SecondaryCopywriter> list = null;
        return Result.fail(list).message("对象为空");
    }


    @ApiOperation(value = "修改二级文案")
    @PostMapping("/updateSecondaryCopywriter")
    public Result<SecondaryCopywriter> updateSecondaryCopywriter(@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setUpdateTime(LocalDateTime.now());

        boolean b = secondaryCopywriterService.updateById(secondaryCopywriter);

        if (b){
            return Result.ok(secondaryCopywriter).message("修改成功");
        }
        return Result.fail(secondaryCopywriter).message("修改失败");
    }

    @ApiOperation(value = "删除二级文案")
    @PostMapping("delectSecondaryCopywriter/{id}")
    public Result delectSecondaryCopywriter(@PathVariable Integer id){

        boolean b = secondaryCopywriterService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "添加关于润泰二级文案")
    @PostMapping("/saveSecondaryCopywriter/{id}")
    public Result<SecondaryCopywriter> saveSecondaryCopywriter(@PathVariable Integer id,@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setCreateTime(LocalDateTime.now());
        secondaryCopywriter.setStartid(1);
        secondaryCopywriter.setMainArgumentId(id);
        boolean save = secondaryCopywriterService.save(secondaryCopywriter);

        if (save){
            return Result.ok(secondaryCopywriter).message("添加成功");
        }
        return Result.fail(secondaryCopywriter).message("添加失败");
    }

//    @ApiOperation(value = "查看所有关于润泰一、二级文案")
//    @GetMapping("/getInRegardOfListSC/{startId}")
//    public Result<List<InRegardOf>> getInRegardOfByList(@PathVariable Integer startId){
//        List<InRegardOf> list = inRegardOfService.list();
//        for (int i = 0; i < list.size(); i++) {
//            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,list.get(i).getId());
//            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper);
//            list.get(i).setSecondaryCopywriterList(copywriterList);
//        }
//        return Result.ok(list);
//    }



    @ApiOperation(value = "查看所有关于润泰一、二级文案")
    @GetMapping("/getInRegardOfListSC/{startId}")
    public Result<IPage<InRegardOf>> getInRegardOfByList(@PathVariable Integer startId,@RequestParam(required = false) Long page, @RequestParam(required = false) Long limit){
        List<InRegardOf> list = inRegardOfService.list();

        Page<InRegardOf> recommendPage = new Page<>(page,limit);
        LambdaQueryWrapper<InRegardOf> wrapper = new LambdaQueryWrapper<>();
        Integer id = null;
        for (InRegardOf regardOf : list) {
            id = regardOf.getId();
        }
        if (id!=null){
            wrapper.eq(InRegardOf::getId,id).orderByDesc(InRegardOf::getCreateTime);
        }
        IPage<InRegardOf> iPage = inRegardOfService.page(recommendPage,wrapper);
        for (InRegardOf record : iPage.getRecords()) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,record.getId());
            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper1);
            record.setSecondaryCopywriterList(copywriterList);
        }
        return Result.ok(iPage);
    }


    @ApiOperation(value = "修改关于润泰")
    @PostMapping("/updateInRegardOf")
    public Result<InRegardOf> updateInRegardOf(@RequestBody InRegardOf inRegardOf){

        inRegardOf.setUpdateTime(LocalDateTime.now());

        boolean b = inRegardOfService.updateById(inRegardOf);

        if (b){
            return Result.ok(inRegardOf).message("修改成功");
        }
        return Result.fail(inRegardOf).message("修改失败");
    }


    @ApiOperation(value = "添加关于润泰")
    @PostMapping("/saveInRegardOf")
    public Result<InRegardOf> saveInRegardOf(@RequestBody InRegardOf inRegardOf){

        inRegardOf.setCreateTime(LocalDateTime.now());
        boolean save = inRegardOfService.save(inRegardOf);

        if (save){
            return Result.ok(inRegardOf).message("添加成功");
        }
        return Result.fail(inRegardOf).message("添加失败");
    }

    @ApiOperation(value = "删除关于润泰文章")
    @PostMapping("delectInRegardOf/{id}")
    public Result delectInRegardOf(@PathVariable Integer id){

        boolean b = inRegardOfService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询润泰文章")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<InRegardOf>> indexda(@PathVariable Long page, @PathVariable Long limit, InRegardOf inRegardOf){

        Page<InRegardOf> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<InRegardOf> wrapper = new LambdaQueryWrapper<>();

        Integer id = inRegardOf.getId();

        if (id!=null){
            wrapper.eq(InRegardOf::getId,id);
        }

        IPage<InRegardOf> iPage = inRegardOfService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }


    @ApiOperation(value = "添加润泰文章收藏")
    @PostMapping("/collectInRegardOf")
    public Result<ArticleCollect> collectInRegardOf(@RequestBody InRegardOf ipaEnter) {

        String s = AuthContextUtil.get();

        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, ipaEnter.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,1);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one!=null){
            return Result.ok(one);
        }else {
            ArticleCollect produceCollect = new ArticleCollect();
            produceCollect.setCreateTime(LocalDateTime.now());
            produceCollect.setInRegardOfId(ipaEnter.getId());
            produceCollect.setAppletUserId(Integer.valueOf(s));
            produceCollect.setStartId(1);
            boolean save = articleCollectService.save(produceCollect);

            if (save) {
                return Result.ok(produceCollect).message("添加成功");
            } else {
                return Result.ok(produceCollect).message("添加失败");
            }
        }
    }

    @ApiOperation(value = "删除通讯录收藏")
    @PostMapping("delectIpaEnterCollect")
    public Result delectIpaEnterCollect(@RequestBody InRegardOf ipaEnter) {
        String s = AuthContextUtil.get();

        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, ipaEnter.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,1);
        boolean b = articleCollectService.removeById(wrapper);

        if (b) {
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

}

